﻿using System;
using System.Runtime.InteropServices;

namespace LDSModel.Item
{
    [StructLayout(LayoutKind.Sequential)]
    public class MsgItem
    {
        public UInt16 SrcAddr;
        public UInt16 DestAddr;
        public UInt16 MsgId;
        public UInt16 DataLength;
        public UInt16 MsgType;
        public UInt16 Exception;
        public byte[] Buffer;

        public MsgItem(MsgHead head, byte[] bytes)
        {
            SrcAddr = head.SrcAddr;
            DestAddr = head.DestAddr;
            MsgId = head.MsgID;
            DataLength = head.DataLength;
            MsgType = head.MsgType;
            Exception = head.Exception;

            Buffer = bytes.Clone() as byte[];
        }
    }

    /// <summary>
    /// 网络头信息结构体。
    /// </summary>
    [Serializable]
    [StructLayout(LayoutKind.Explicit)]
    public class MsgHead
    {
        [FieldOffset(0)]
        public UInt16 FrameStart = 0xAA55;

        [FieldOffset(2)]
        public UInt16 RelayAddr = 0;

        [FieldOffset(4)]
        public UInt16 SrcAddr = 0;

        [FieldOffset(6)]
        public UInt16 DestAddr = 0;

        [FieldOffset(8)]
        public UInt16 MsgID = 0;

        [FieldOffset(10)]
        public UInt16 DataLength = 0;

        [FieldOffset(12)]
        public UInt16 MsgType = 0;

        [FieldOffset(14)]
        public UInt16 Exception = 0;
    }
}
